import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn import preprocessing
from sklearn.linear_model import Lasso

if __name__ == '__main__':
    path = 'data/boston_housing_data.csv'
    boston_df = pd.read_csv(path)

    # 可修改特征array: 要分割的数组; indices_or_sections: 一个整数或者序列，表示分割的方式：axis: 指定沿着哪个轴进行分割。默认是0，表示沿着第一个轴（通常是行）进行分割。如果设置为1，则沿着第二个轴（通常是列）进行分割。
    x, y = np.split(boston_df, (13,), axis=1)

    # 多一步：选择合适的特征
    selector = SelectKBest(score_func=f_regression, k=12)
    X_selected = selector.fit_transform(x, y)

    selected_features = list(x.columns[selector.get_support()])
    print("selected_features:{}".format(selected_features))

    x = boston_df[selected_features]
    x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8, random_state=1)

    # 初始化标准化器
    min_max_scaler = preprocessing.MinMaxScaler()
    # 分别对训练和测试数据的特征以及目标值进行标准化处理
    x_train = min_max_scaler.fit_transform(x_train)
    y_train = min_max_scaler.fit_transform(y_train)  # reshape(-1,1)指将它转化为1列，行自动确定
    x_test = min_max_scaler.fit_transform(x_test)
    y_test = min_max_scaler.fit_transform(y_test)

    # L1正则化
    lasso_model = Lasso(alpha=0.00001)
    lasso_model.fit(x_train, y_train)
    mse_lasso = mean_squared_error(y_test, lasso_model.predict(x_test))
    print("mse_lasso=", mse_lasso)
